using System;
using System.Linq;

namespace Picnic.Spec.Console.ExceptionWriters
{
	/// <summary>
	/// Represents a writer for System.Exception.
	/// </summary>
	public class DefaultExceptionWriter : AbstractExceptionWriter
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="DefaultExceptionWriter"/> class.
		/// </summary>
		public DefaultExceptionWriter(ConsoleColor color = ConsoleColor.Red) : base(color)
		{
		}

		/// <summary>
		/// Determines is writer can write specified exception.
		/// </summary>
		public override bool CanWrite(Exception exception)
		{
			return true;
		}

		protected override void WriteException(IConsole console, Exception exception)
		{
			var lines = exception.ToString().Split('\n').Select(l => l.Trim());
			foreach (var line in lines)
			{
				console.WriteLine(line);
			}
		}
	}
}